﻿Imports System.Data.SqlClient.SqlConnection
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class DatabaseConfig
    Public conStr As String = Nothing
    Private Function testPrerequis() As Boolean
        Dim errorText As String = ""
        Dim state As Boolean = False
        If Not txtServerName.Text.Length > 0 Then
            errorText += "Le nom du serveur est obligatoire" + "\n"
            State = True
        End If
        If Not txtDatabaseName.Text.Length > 0 Then
            errorText += "Le nom de la base de donnée est obligatoire" + "\n"
            State = True
        End If
        If Not txtUserName.Text.Length > 0 Then
            errorText += "Le nom d'utilisateur est obligatoire" + "\n"
            state = True
        End If
        Return False
    End Function
    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        Cursor = Cursors.WaitCursor
        Dim state As Boolean = testPrerequis()
        

        If state = False Then
            conStr = "Data Source=" + txtServerName.Text + ";Initial Catalog=" + txtDatabaseName.Text + ";User Id=" + txtUserName.Text + ";Password=" + pbPassword.Text + ""
            Dim con As SqlConnection = New SqlConnection(conStr)
            'Dim datCon As OleDbConnection
            'Try
            '    'Initialize datCon object
            '    datCon = New OleDbConnection(conStr)
            '    MessageBox.Show("Connexion réussie !!")
            'Catch ex As Exception
            '    MessageBox.Show("Echec de connexion !! Détails :" & ex.Message)
            '    conStr = Nothing
            'End Try
            Try

                con.Open()
                con.Close()

                MessageBox.Show("Connexion réussie !!")

            Catch ex As Exception
                MessageBox.Show("Echec de connexion !! Détails :" & ex.Message)
                conStr = Nothing
            End Try
        End If
        Cursor = Cursors.Arrow
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim state As Boolean = testPrerequis()


        If state = False Then
            Try
                conStr = "Data Source=" + txtServerName.Text + ";Initial Catalog=" + txtDatabaseName.Text + ";User Id=" + txtUserName.Text + ";Password=" + pbPassword.Text + ""
                Dim con As SqlConnection = New SqlConnection(conStr)
                con.Open()
                con.Close()
                Me.Close()
                ' MessageBox.Show("Connexion réussie !!")

            Catch ex As Exception
                MessageBox.Show("Echec de connexion !! Détails :" & ex.Message)
                conStr = Nothing
            End Try

        End If

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
End Class